/* Copyright (c) 2025 Beijing Semidrive Technology Corporation
 * SPDX-License-Identifier: Apache-2.0
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/** *****************************************************************************************************
 * \file     Eep.h                                                                            *
 * \brief    AUTOSAR 4.3.1 MCAL mram Driver                                                             *
 *                                                                                                      *
 * <table>                                                                                              *
 * <tr><th>Date           <th>Version                                                                   *
 * <tr><td>2024/11/04     <td>1.0.0                               *
 * </table>                                                                                             *
 *******************************************************************************************************/

/** *****************************************************************************************************
 *  \file     Eep.h                                                                           *
 *  \brief    This file contains interface header for CAN MCAL driver, ...                             *
 *                                                                                                      *
 * <table>                                                                                              *
 * <tr><th>Date           <th>Version                                                                   *
 * <tr><td>2024/11/04     <td>1.0.0                               *
 * </table>                                                                                             *
 *******************************************************************************************************/

#ifndef EEP_H
#define EEP_H

#include "Mcal.h"
#include "MemIf_Types.h"
#include "Std_Types.h"
#include "Eep_Cfg.h"
#include "Mram_Ip.h"

/* PRQA S 1039 EOF */

#ifdef __cplusplus
extern "C" {
#endif

/********************************************************************************************************
 *                                 Global Macro definition                                              *
 *******************************************************************************************************/
/* Development Errors */
#define EEP_E_INIT_FAILED                   (0x10u)
#define EEP_E_PARAM_ADDRESS                 (0x11u)
#define EEP_E_PARAM_DATA                    (0x12u)
#define EEP_E_PARAM_LENGTH                  (0x13u)
#define EEP_E_PARAM_POINTER                 (0x23u) /* API service called with a NULL pointer*/
#define EEP_E_UNINIT                        (0x20u) /* API service called without module initialization */

/* Runtime Errors */
#define EEP_E_BUSY                          (0x21u) /* API service called while driver still busy */
#define EEP_E_TIMEOUT                       (0x22u) /* Timeout exceeded */

#define EEP_MODULE_ID                       (90u)

#define EEP_CRC_AUTO_ACQUISITION            (0xFFFFFFFFu)

/* Extended Production Errors */
typedef enum
{
    EEP_E_NO_ERROR = 0u,
    EEP_E_ERASE_FAILED = 1u,
    EEP_E_WRITE_FAILED,
    EEP_E_READ_FAILED,
    EEP_E_COMPARE_FAILED,
    EEP_E_OPERATION_CANCEL,
    EEP_E_OPERATION_TIMEOUT,
    EEP_E_EXT_PRODUCT_ERROR_MAX
} Eep_ExtendedProductionErrorType;

typedef enum
{
    EEP_INIT_SERVICE_ID             = 0x00u,
    EEP_SET_MODE_SERVICE_ID         = 0x01u,
    EEP_READ_SERVICE_ID             = 0x02u,
    EEP_WRITE_SERVICE_ID            = 0x03u,
    EEP_ERASE_SERVICE_ID            = 0x04u,
    EEP_COMPARE_SERVICE_ID          = 0x05u,
    EEP_CANCEL_SERVICE_ID           = 0x06u,
    EEP_GET_STATUS_SERVICE_ID       = 0x07u,
    EEP_GET_JOBRESULT_SERVICE_ID    = 0x08u,
    EEP_MAINFUNCTION_SERVICE_ID     = 0x09u,
    EEP_GET_VERSION_SERVICE_ID      = 0x0Au,
    EEP_SERVICE_ID_MAX              = 0xFFu
} Eep_InterfaceServiceIdType;

/* Used as address offset from the configured EEPROM base address to access a certain EEPROM memory area. */
typedef uint32 Eep_AddressType;
/* Specifies the number of bytes to read/write/erase/compare. */
typedef uint32 Eep_LengthType;
/* This callback function provided by the module user is called when a job has been completed with a positive result. */
typedef void (*Eep_JobEndNotificationType)(void);
/* This callback function provided by the module user is called when a job has been canceled or finished with negative result */
typedef void (*Eep_JobErrorNotificationType)(void);
/* This callback function provided by the module user is called when Low-level event reporting */
typedef void (*Eep_EventNotificationType)(uint32 event);

typedef struct
{
    Eep_AddressType baseAddress;
    Eep_LengthType size;
    Eep_JobEndNotificationType jobEndNotification;
    Eep_JobErrorNotificationType jobErrorNotification;
    uint8 priority;
} Eep_BlockConfigType;

typedef struct
{
    Eep_AddressType address;
    Eep_LengthType size;
} Eep_AreaInfoType;

/* This is the type of the external data structure containing the initialization data for the EEPROM driver. */
typedef struct
{
    MemIf_ModeType defaultMode;
    Eep_LengthType fastReadBlockSize;
    Eep_LengthType fastWriteBlockSize;
    Eep_LengthType normalReadBlockSize;
    Eep_LengthType normalWriteBlockSize;
    Eep_BlockConfigType blockConfig[EEP_CFG_INIT_BLOCK_NUM];
#if (EEP_CFG_PROTECT_AREA_NUM)
    Eep_AreaInfoType protectArea[EEP_CFG_PROTECT_AREA_NUM];
#endif /* #if (EEP_CFG_PROTECT_AREA_NUM) */
#if (EEP_CFG_IMAGE_AREA_NUM)
    Eep_AreaInfoType imageArea[EEP_CFG_IMAGE_AREA_NUM];
#endif /* #if (EEP_CFG_IMAGE_AREA_NUM) */
} Eep_InitConfigType;

typedef struct
{
    uint8 deviceId;
    Mram_HostConfigType hostConfig;
    Eep_EventNotificationType eventNotification;
    const Mram_RdBufferCfgType *rdBufferConfigs;
    uint8 rdBufferConfigsNum;
    uint8 semagID;
} Eep_ControllerConfigType;

/* This is the type of the external data structure containing the initialization data for the EEPROM driver. */
typedef struct
{
    Eep_InitConfigType initConfig;
    Eep_ControllerConfigType controllerConfig[EEP_CFG_HOST_MAX_NUM];
} Eep_ConfigType;

typedef struct Eep_HostContext
{
    Eep_AddressType baseAddress;
    Eep_LengthType size;
    Mram_HostType mramHost;
    const Eep_ControllerConfigType *config;
} Eep_HostContextType;

typedef struct
{
    /* required */
    Eep_AreaInfoType area;
    uint32 crcExpval;

    /* Automatic generation at Eep_ImageCrcConfig() */
    Eep_HostContextType *hostCtx;
    uint8 channelId;
} EepImageInfoType;

/********************************************************************************************************
 *                                  Global Variable Declarations                                        *
 *******************************************************************************************************/
extern const Eep_ConfigType Eep_ConfigData;
#if (EEP_CFG_IMAGE_AREA_NUM)
extern EepImageInfoType Eep_ImageInfo[EEP_CFG_IMAGE_AREA_NUM];
#endif /* EEP_CFG_IMAGE_AREA_NUM */

/********************************************************************************************************
 *                                  Global Function Declarations                                        *
 *******************************************************************************************************/

void Eep_Init(const Eep_ConfigType* ConfigPtr);

void Eep_SetMode(MemIf_ModeType Mode);

Std_ReturnType Eep_Read(Eep_AddressType EepromAddress, uint8* DataBufferPtr,
                            Eep_LengthType Length);

Std_ReturnType Eep_Write(Eep_AddressType EepromAddress, const uint8* DataBufferPtr,
                            Eep_LengthType Length);


Std_ReturnType Eep_Erase(Eep_AddressType EepromAddress, Eep_LengthType Length);


Std_ReturnType Eep_Compare(Eep_AddressType EepromAddress, const uint8* DataBufferPtr,
                            Eep_LengthType Length);

void Eep_Cancel(void);

MemIf_StatusType Eep_GetStatus(void);

MemIf_JobResultType Eep_GetLastJobResult(void);

MemIf_JobResultType Eep_GetJobResultById(uint8 index);

MemIf_JobResultType Eep_GetJobResult(void);

void Eep_MainFunction(void);

#if (EEP_VERSIONINFO_API == STD_ON)
void Eep_GetVersionInfo(Std_VersionInfoType* versioninfo);
#endif /* #if (EEP_VERSIONINFO_API == STD_ON) */

Std_ReturnType Eep_ImageCrcConfig(EepImageInfoType *imageInfo);

Std_ReturnType Eep_ImageCrcCheckStart(EepImageInfoType *imageInfo, Mram_CrcCheckModeType mode);

Eep_HostContextType *Eep_GetHostContextById(uint8 controllerId);

Std_ReturnType Eep_ClockConfig(Eep_HostContextType *hostCtx, uint32 tmcClk);

Std_ReturnType Eep_Protect(Eep_AddressType address, Eep_LengthType size, boolean enable);

Std_ReturnType Eep_EccRepair(void);

void Eep_Deinit(void);

#if (EEP_USE_INTERRUPTS == STD_ON)
#define EEP_IP_IRQ_FUNC_NAME(n) Eep_IrqHandlerController##n
ISR(Eep_IrqHandlerController0);
ISR(Eep_IrqHandlerController1);
ISR(Eep_IrqHandlerController2);
ISR(Eep_IrqHandlerController3);
ISR(Eep_IrqHandlerController4);
void Eep_IrqEventSetup(uint8 controllerId, Eep_HostContextType *hostCtx);
void Eep_IrqEventDeinit(void);
#endif /* #if (EEP_USE_INTERRUPTS == STD_ON) */

#ifdef __cplusplus
}
#endif
#endif /* EEP_H */



/* End of file */
